package handler

import (
	"encoding/json"
	"logi-hub/cache"
	"logi-hub/models"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var order models.DepotOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("depots").Select(
			"depot_orders.id AS OrderId",
			"depots.depot_name AS DepotName",
			"depots.depot_type AS DepotType",
			"depots.depot_content AS DepotContent",
			"depot_orders.order_code AS OrderCode",
			"depot_orders.order_num AS OrderNum",
			"depot_orders.order_price AS OrderPrice",
		).Joins("LEFT JOIN depot_orders ON depots.id=depot_orders.id").
			Where("depot_orders.id=?", id).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": order,
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var order []models.DepotOrderList
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	//SELECT
	//depot_orders.id AS OrderId,
	//depots.depot_name AS DepotName,
	//depots.depot_type AS DepotType,
	//depots.depot_content AS DepotContent,
	//depot_orders.order_code AS OrderCode,
	//depot_orders.order_num AS OrderNum,
	//depot_orders.order_price AS OrderPrice
	//FROM
	//depots
	//LEFT JOIN depot_orders ON depots.id=depot_orders.id
	if cacheData == "" {
		models.DB.Table("depots").Select(
			"depot_orders.id AS OrderId",
			"depots.depot_name AS DepotName",
			"depots.depot_type AS DepotType",
			"depots.depot_content AS DepotContent",
			"depot_orders.order_code AS OrderCode",
			"depot_orders.order_num AS OrderNum",
			"depot_orders.order_price AS OrderPrice",
		).Joins("LEFT JOIN depot_orders ON depots.id=depot_orders.id").
			Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	var total int64
	models.DB.Model(&models.DepotOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  order,
		"total": total,
	})
	return
}
func DepotOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_num := c.PostForm("order_num")
	order_price := c.PostForm("order_price")
	data := models.DepotOrder{
		Model:      gorm.Model{},
		OrderCode:  order_code,
		OrderNum:   order_num,
		OrderPrice: order_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func DepotAdd(c *gin.Context) {
	depot_name := c.PostForm("depot_name")
	depot_type := c.PostForm("depot_type")
	depot_content := c.PostForm("depot_content")
	data := models.Depot{
		Model:        gorm.Model{},
		DepotName:    depot_name,
		DepotType:    depot_type,
		DepotContent: depot_content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
